package runesim.node.connectors;
import org.xsocket.connection.*;
public class LSConnection {
	
	public static void main(String[] args){
		connect();
	}
	
	static INonBlockingConnection connection;
	static LSConnectionHandle lsch;
	static boolean connected = false;
	
	static {
		lsch = new LSConnectionHandle();
	}
	
	public static boolean isConnected() {
		return connected;
	}
	
	public static INonBlockingConnection getConnection() {
		return connection;
	}
	
	public static void setConnected(boolean state) {
		connected = state;
	}
	
	public static void forceDisconnect() {
		try {
			if(isConnected()) {
				getConnection().close();
				setConnected(false);
				System.out.println("Connection was forcefully closed");
			} else {
				throw new Exception();
			}
		} catch(Exception e) {
			System.out.println("Can't force close connection!");
		}
	}
	
	public static void connect() {
		if(connected) {
			System.out.println("Already connected to login server.");
		} else {
			try {
				connection = new NonBlockingConnection("localhost", 43595, lsch);
				connection.write(LSConnectionHandle.OUT_HELLO);
				connection.write((byte) 1); //Node ID
				System.out.println("Established login server connection.");
			} catch(Exception e) {
				System.out.println("Failed to connect to login server! Still not connected!");
				connected = false;
			}
		}
	}
}